<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="All Rights Reserved, Copyright (C) 2013, Wuyeguo, Ltd." />
<title>EasyUI Web Admin Power by Wuyeguo</title>
<link rel="stylesheet" type="text/css" href="../easyui/1.5.3/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../easyui/1.5.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../easyui/1.5.3/locale/easyui-lang-zh_CN.js"></script>

<!-- <script type="text/javascript" src="../public/xheditor/jquery/jquery-1.4.4.min.js"></script> -->
<script type="text/javascript" src="../public/xheditor/xheditor-1.2.2.min.js"></script>
<!-- <script type="text/javascript" src="../public/xheditor/xheditor_lang/zh-cn.js"></script> -->
</head>
<body class="easyui-layout">
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'west',border:true,split:true," title="新闻管理" style="width:150px; padding:5px;">
        <ul id="tt"></ul>
    </div>
    <div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
		<table id="dg" style="margin-top:5px;min-height:222px;" class="easyui-datagrid" toolbar="#wu-toolbar-2"></table>
	</div>

</div>

<div id="news-dialog-2" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
	<form id="news-form-2" method="post">
        <table>
            <tr>
                <td width="60" align="right">信息标题:</td>
                <td><input type="text" name="title" class="wu-text wu-text0" /></td>
            </tr>
            <tr>
                <td align="right">作 者:</td>
                <td><input type="text" name="author" class="wu-text wu-text1" /></td>
            </tr>
            <tr>
                <td align="right">副标题:</td>
                <td><input type="text" name="htitle" class="wu-text wu-text22" /></td>
            </tr>
            <tr>
                <td align="right">内 容:</td>
                <td>
                <!-- <textarea name="content" rows="6" class="" style=""></textarea> -->
                <textarea name="content" rows="6" style="width:260px" id="content" class="xheditor wu-textarea wu-text32"></textarea>
                </td>
            </tr>
            <tr>
                <td valign="top" align="right">所属分类:</td>
                <td><input type="text" name="belongs" class="wu-text wu-text42" /></td>
            </tr>
        </table>
    </form>
</div>

<script type="text/javascript">

	$('#tt').tree({
		animate:true,
		url:'http://localhost:3000/cate/list',
		method:'get',
		onClick:function(node){
			initList(node._id)
		}
	})



	function initList(cateId){
		var postUrl;
		if(cateId !=undefined && cateId.trim().length>0){
			queryParams = {cateId: cateId}
		}else{
			queryParams = {};
		}

		$("#dg").datagrid({
			url:'http://localhost:3000/news/list',
			method:'post',
			pagination:true,
			pageSize:6,
			fit:true,
			fitColumns:true,
			queryParams: queryParams,
			pageList:[2,4,6,8,10],
			columns:[[
		        {field:'title',title:'信息标题',width:00},
		        { field:'author',title:'作者',width:00},
				{ field:'htitle',title:'副标题',width:00},
				{ field:'content',title:'内容',width:0},
				{ field:'belongs',title:'所属分类',width:00},
				{ field:'date',title:'发布时间',width:00},
				{field:'operate',title:'管理', width:200,
					formatter: function(value,row,index){
						return '<a href="javascript:void(0)" class="easyui-linkbutton link" onclick="addTab('+"'评论管理'"+','+"'temp/layout-3.html?id="+row._id+"'"+',null,1)"> 查看详情</a>'+
					'<a href="javascript:void(0)" class="easyui-linkbutton editcls" style="padding:0 15px;" onclick="editUser(\''+ row._id+'\')">编辑</a>'+
					'<a href="javascript:void(0)" class="easyui-linkbutton remove" onclick="deleUser(\''+ row._id+'\')">删除</a>'
					}
				}
		    ]],
		    onLoadSuccess:function(data){  
	            $('.editcls').linkbutton({text:'编辑',plain:true,iconCls:'icon-edit'});
	            $('.remove').linkbutton({text:'删除',plain:true,iconCls:'icon-remove'});
	            $('.link').linkbutton({text:'查看详情',plain:true,iconCls:'icon-search'});
	        }
		})
	}

	initList();






	function editUser(id){
	$("#news-form-2").form("clear");
		$('#news-dialog-2').dialog({
			closed: false,
			modal:true,
            title: "修改信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userAddAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#news-dialog-2').dialog('close');
                }
            }]
        });
        $.ajax({
        	type:"post",
			url:"http://localhost:3000/news/all"
        }).then(function(res){
        	for(var i=0;i<res.length;i++){
        		if(res[i]._id==id){
        			$(".wu-text0").val(res[i].title);
					$(".wu-text1").val(res[i].author);
					$(".wu-text22").val(res[i].htitle);
					$(".wu-text32").val(res[i].content);
					$(".wu-text42").val(res[i].belongs);
        		}
        	}
        })
	}
	function userAddAction(id){
		var node = $('#dg').datagrid('getSelected');
		var id=node._id;
		var postUrl;
		var formData = $('#news-form-2').serializeJSON();
		$.ajax({
			type:"post",
			url:"http://localhost:3000/news/data/"+id,
			data: formData
		}).then(function(res){
			$('#dg').datagrid('reload');
			$('#news-dialog-2').dialog('close');
		})
	}


	function deleUser(id){
		$.ajax({
			type:"delete",
			url:"http://localhost:3000/news/data/"+ id
		}).then(function(res){
			$('#dg').datagrid('reload');
		});
	}



	function addTab(title, href, iconCls, iframe){
		var tabPanel = parent.$('#wu-tabs');
		if(!tabPanel.tabs('exists',title)){
			var content = '<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>';
			if(iframe){
				tabPanel.tabs('add',{
					title:title,
					content:content,
					iconCls:iconCls,
					fit:true,
					cls:'pd3',
					closable:true
				});
			}
			else{
				tabPanel.tabs('add',{
					title:title,
					href:href,
					iconCls:iconCls,
					fit:true,
					cls:'pd3',
					closable:true
				});
			}
		}
		else
		{
			tabPanel.tabs('select',title);
			var current_tab=tabPanel.tabs('getSelected');
			tabPanel.tabs('update',{
				tab:current_tab,
				options:{
					content:'<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>'
				}
			})
		}
	}


</script>
</body>
</html>


















<!-- <div class="easyui-layout" data-options="fit:true" >
<div id="wu-toolbar-2">
<div class="user-toolbar-button">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openAddUser()" plain="true">添加</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="openEditUser()" plain="true">修改</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeUser()" plain="true">删除</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reloadUser()" plain="true">刷新</a>
</div>
<div class="user-toolbar-search">
    <label>起始时间：</label><input class="easyui-datebox" style="width:100px">
    <label>结束时间：</label><input class="easyui-datebox" style="width:100px">
    <label>用户组：</label> 
    <select class="easyui-combobox" panelHeight="auto" style="width:100px">
        <option value="0">选择用户组</option>
        <option value="1">黄钻</option>
        <option value="2">红钻</option>
        <option value="3">蓝钻</option>
    </select>
    <label>关键词：</label><input class="wu-text" style="width:100px">
    <a href="#" class="easyui-linkbutton" iconCls="icon-search">开始检索</a>
</div>
</div>

<table id="user-datagrid-2" class="easyui-datagrid" toolbar="#wu-toolbar-2"></table>
</div>
<div id="user-dialog-2" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:400px; padding:10px;">
	<form id="user-form-2" method="post">
        <table>
            <tr>
                <td width="60" align="right">标 题:</td>
                <td><input type="text" name="title" class="wu-text" /></td>
            </tr>
            <tr>
                <td align="right">作 者:</td>
                <td><input type="text" name="author" class="wu-text" /></td>
            </tr>
            <tr>
                <td align="right">副标题:</td>
                <td><input type="text" name="htitle" class="wu-text" /></td>
            </tr>
            <tr>
                <td valign="top" align="right">内 容:</td>
                <td><textarea name="content" rows="6" class="wu-textarea" style="width:260px"></textarea></td>
            </tr>
            <tr>
                <td align="right">所属分类:</td>
                <td><input type="text" name="belongs" class="wu-text" /></td>
            </tr>
        </table>
    </form>
</div>

<script type="text/javascript">
	$('#user-datagrid-2').datagrid({
		url:'http://localhost:3000/news/list',
		method:'post',
		pagination:true,
		fit:true,
		columns:[[
			{ checkbox:true},
			{ field:'title',title:'标题',width:100},
			{ field:'author',title:'作者',width:110},
			{ field:'htitle',title:'副标题',width:100},
			{ field:'content',title:'内容',width:100},
			{ field:'belongs',title:'所属分类',width:100},
			{ field:'date',title:'发布时间',width:100},
			{field:'operate',title:'管理', width:80,
				formatter: function(value,row,index){
					// console.log(index)
					return '<a href="javascript:void(0)" onclick="editUser(\''+ row._id+'\')">修改</a> '+
				'<a href="javascript:void(0)" onclick="deleUser(\''+ row._id+'\')">删除</a>'

				}
			}
		]]
	});

	function openAddUser(){
		$('#user-form-2').form('clear');
		$('#user-dialog-2').dialog({
			closed: false,
			modal:true,
            title: "添加信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: userAddAction
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#user-dialog-2').dialog('close');
                }
            }]
        });
	}

	function userAddAction(){
		$('#user-form-2').form('submit',{
			onSubmit:function(){
				// return $(this).form('enableValidation').form('validate')
				if($(this).form('enableValidation').form('validate')){
					// console.log($('#user-form-2').serializeJSON())
					$.ajax({
						type:'post',
						url:'http://localhost:3000/news/data',
						data:$('#user-form-2').serializeJSON()
					}).then(function(res){
						console.log(res);
					})
				}else{
					alert('no')
				}
			}
		})
	}

	function removeUser(){
		$.messager.confirm('信息提示','确定要删除该记录？', function(result){
			if(result){
				var items = $('#user-datagrid-2').datagrid('getSelections');
				var ids = [];
				$(items).each(function(){
					ids.push(this.productid);
				});
				//alert(ids);return;
				$.ajax({
					url:'',
					data:'',
					success:function(data){
						if(data){
							$.messager.alert('信息提示','删除成功！','info');
						}
						else
						{
							$.messager.alert('信息提示','删除失败！','info');
						}
					}
				});
			}
		});
	}

	function openEditUser(){
		$('#user-form-2').form('clear');
		var item = $('#user-datagrid-2').datagrid('getSelected');
		//alert(item.productid);return;
		$.ajax({
			url:'',
			data:'',
			success:function(data){
				if(data){
					$('#user-dialog-2').dialog('close');	
				}
				else{
					//绑定值
					$('#user-form-2').form('load', data)
				}
			}	
		});
		$('#user-dialog-2').dialog({
			closed: false,
			modal:true,
            title: "修改信息",
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: edit
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#user-dialog-2').dialog('close');
                }
            }]
        });
	}

	function editUser(id){
		console.log(id);
		// return false;
	}
	function deleUser(id){
		$.ajax({
			type:"delete",
			url:"http://localhost:3000/news/data/"+ id
		}).then(function(res){
			$('#user-datagrid-2').datagrid('reload');
		});
	}


	function pagerFilter(data){
		if (typeof data.length == 'number' && typeof data.splice == 'function'){// is array
			data = {
				total: data.length,
				rows: data
			}
		}
		var dg = $(this);
		var opts = dg.datagrid('options');
		var pager = dg.datagrid('getPager');
		pager.pagination({
			onSelectPage:function(pageNum, pageSize){
				opts.pageNumber = pageNum;
				opts.pageSize = pageSize;
				pager.pagination('refresh',{pageNumber:pageNum,pageSize:pageSize});
				dg.datagrid('loadData',data);
			}
		});
		if (!data.originalRows){
			data.originalRows = (data.rows);
		}
		var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
		var end = start + parseInt(opts.pageSize);
		data.rows = (data.originalRows.slice(start, end));
		return data;
	}
</script> -->